import { UPLOAD_PATH } from '@/config/path'
/**
 * 将图片保存到本地相册
 * @param {图片路径} path
 */
const savePhotoAlbum = (path) => {
    uni.downloadFile({
        url: path,
        success: (res) => {
            if (res.statusCode === 200) {
                uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: () => {
                        uni.showToast({ title: "保存成功" });
                    },
                    fail: (fail) => {
                        console.log(fail, "失败");
                    },
                    complete: (complete) => {
                        console.log(complete, "执行");
                    },
                });
            }
        },
    });
};

/**
 * 预览图片
 * @param {图片数据} urls
 * @param {默认索引} current
 */
const previewImgFn = (urls, current) => {
    uni.previewImage({
        urls,
        current,
    });
};


// 选择图片
const chooseImageFn = () => {
    return new Promise((resolve, reject) => {
        uni.chooseImage({
            count: 1,
            // sourceType:sourceOption,
            success: (res) => {
                resolve(res.tempFilePaths[0]);
            },
            fail(err) {
                reject(err);
            },
            complete() {},
        });
    });
};

/**
 * 上传图片的远程服务器
 * @param {图片路径} filePath
 */
const uploadFn = (filePath) => {
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: 'https://aicenter.luyantech.com/api/file/upload_new',
            filePath,
            name: "file",
            header: {
                uuid: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiMTAwMTA0OCJ9.Tdh1By5nBN5-4N5-JQw3nVVz_OFk4D_SXRZuaWqz96w'
            },
            success: (res) => {
                if (res.statusCode !== 200) {
                    uni.showToast({
                        title: "网络出错，请稍后再试",
                        icon: "none",
                    });
                    return;
                }
                const dataObject = JSON.parse(res?.data);
                if (dataObject.code > 0) {
                    uni.showToast({ title: dataObject.msg, icon: "error" });
                    return;
                }
                let { url } = dataObject.data;
                let path = `${UPLOAD_PATH}/img${url}`
                resolve(path)
                uni.showToast({
                    title: "上传成功",
                    icon: "success",
                });
            },
            fail: function () {
                uni.showToast({
                    icon: "none",
                    title: "上传失败",
                });
                reject(false)
            },
            complete: (res) => {
   
            },
        });
    });
};





// 获取图片的本地临时路径
const getimgInfoFn = (src) => {
    return new Promise((resolve, reject) => {
        uni.getImageInfo({
            src,
            success: (res) => {
                resolve(res.path);
            },
            fail: (err) => {
                reject(false);
            },
        });
    });
};

export { savePhotoAlbum, previewImgFn, uploadFn, chooseImageFn, getimgInfoFn  };
